package com.example.yz.happywoker;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SignUpCallback;
import com.example.yz.happywoker.lib.ActivityList;

import java.util.List;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mAccount;                        //用户名编辑
    private EditText mPwd;                            //密码编辑
    private EditText mPwdCheck;                       //密码编辑
    private ImageView suggestion_usercontent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ActivityList.getInstance().addActivity(this);

        mAccount = (EditText) findViewById(R.id.resetpwd_edit_name);
        mPwd = (EditText) findViewById(R.id.resetpwd_edit_pwd_old);
        mPwdCheck = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);
        Button mSureButton = (Button) findViewById(R.id.register_btn_get);
        mSureButton.setOnClickListener(this);
        suggestion_usercontent = (ImageView) findViewById(R.id.suggestion_usercontent);
        suggestion_usercontent.setOnClickListener(this);
        TextView userAgree = (TextView) findViewById(R.id.userAgreements);
        userAgree.setOnClickListener(this);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_btn_get://确认按钮的监听事件
                attempRegister();
                break;
            case R.id.userAgreements://确认按钮的监听事件
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, UserAgreeMentActivity.class);
                startActivity(intent);
                break;

            case R.id.suggestion_usercontent://确认按钮的监听事件
              finish();
                break;
        }
    }
    private void attempRegister() {
       final String userName = mAccount.getText().toString();
        final  String userPwd = mPwd.getText().toString();
        String userPwdCheck = mPwdCheck.getText().toString();
        if (userName.isEmpty() || userPwd.isEmpty() || userPwdCheck.isEmpty()) {
            Toast.makeText(RegisterActivity.this, "输入信息为空", Toast.LENGTH_SHORT).show();
        } else if (!userPwd.equals(userPwdCheck)) {
            Toast.makeText(this, "两次密码不一样", Toast.LENGTH_SHORT).show();
        } else {
            AVQuery<AVUser> userQuery = new AVQuery<>("_User");
            userQuery.whereContains("username", userName);
            userQuery.findInBackground(new FindCallback<AVUser>() {
                @Override
                public void done(List<AVUser> list, AVException e) {
                    // list 返回的就是有图片的 Todo 集合
                    if (list.size()!= 0) {
                        Toast.makeText(RegisterActivity.this, "手机号码已注册", Toast.LENGTH_LONG).show();
                        Log.e("asd123",""+e+list);
                    } else {
                       // Toast.makeText(RegisterActivity.this, "手机号码已注册"+e, Toast.LENGTH_LONG).show();
                        Toast.makeText(RegisterActivity.this, "手机号码可用", Toast.LENGTH_LONG).show();
                        Log.e("asdqwe",""+e+list);
                        Intent intent = new Intent();
                        intent.putExtra("username", userName);
                        intent.putExtra("userPwd", userPwd);
                        intent.setClass(RegisterActivity.this, CompleteInfoActivity.class);
                        startActivity(intent);
                        ActivityList.getInstance().FinishAll();
                    }

                }
            });

        }
    }

    // 回调的方式来获取指定Activity返回的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

    }
}
